<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台配置控制器
 * @author yangweijie <yangweijiester@gmail.com>
 */
class MoneyController extends AdminController {

    public function money(){
            $order_no       =   I('order_no');
            $map_list = array();
            if($order_no){
                $map_list['order_no'] =$order_no;
            }
            $map_list['status'] = "1";
            $investcount = M("Invest")->where(array("status"=>1))->field(array("sum(amount) as totalamount"))->find();
            $this->assign('totalamount', $investcount['totalamount']);
            $list = $this->lists('Invest', $map_list);
            int_to_string($list);
            $this->assign('_list', $list);
            $this->display();
    }
    public function supplier(){
        $selectsupplier = $_GET['supplier_id'];
        $selectstatus = $_GET['status'];
        $start_time = $_GET['start_time'];
        $end_time = $_GET['end_time'];
        $selectgood_id = $_GET['good_id'];
        $search = $_GET['search'];
        if($search== 1 && $selectsupplier == ""){
            $this->error("搜索的时候请选择供应商");
        }
        $memberinfo = M("Member")
            ->join("left join mt_auth_group_access on mt_member.uid=mt_auth_group_access.uid")
            ->where(array("mt_auth_group_access.group_id"=>"6"))->select();

        $this->assign("supplierlist",$memberinfo);
        $this->assign("selectsupplier",$selectsupplier);
        $this->assign("selectstatus",$selectstatus);
        $this->assign("start_time",$start_time);
        $this->assign("end_time",$end_time);
        $this->assign("selectgood_id",$selectgood_id?$selectgood_id:0);
        $memberdata = array();
        foreach($memberinfo as $key=>$val){
            $memberdata[$val['uid']] = $val['nickname'];
        }
        $goodlist = M("Good")->where(array("supplier"=>$selectsupplier))->select();
        $goodsql = "";
        foreach($goodlist as $key=>$val){
            $gooddata[$val['id']]['supplier'] = $val['supplier'];
            $gooddata[$val['id']]['good_name'] = $val['title'];
            //获取该供应商下面的商品
            $goodsql .= 'good_info like \'%"good_id":"'.$val['id'].'"%\' or good_info like \'%"good_id\":\"'.$val['id'].'\"%\' or';
        }
        if($search== 1 &&count($goodlist) == 0){
            $this->error("该供应商无商品");
        }
        $goodsql = substr($goodsql,0,-2);
        //
        if($selectstatus){
            $where["status"] = $selectstatus;
        }
        $where["_string"] = $goodsql;
        if($start_time){
            $where['create_time'] = array("between",array(strtotime($start_time),strtotime($end_time)));
        }

        if($selectsupplier){
            $list = $this->lists('Invest', $where,"create_time desc");
            int_to_string($list);
            $returninfo = array();
            foreach($list as $key=>$val){
                $good_info = json_decode($val['good_info'],true);
                if(!is_array($good_info)){
                    $good_info = json_decode($good_info,true);
                }
                $goodid = $good_info[0]['good_id'];
                $val['supplier_name'] = $memberdata[$gooddata[$goodid]['supplier']];
                $val['good_name'] = $gooddata[$goodid]['good_name'];
                $val['count'] = $good_info[0]['count'];
                if($val['status'] == 1){
                    $val['status_name'] = "完成";
                }elseif($val['status']==3){
                    $val['status_name'] = "待发货";
                }elseif($val['status']==4){
                    $val['status_name'] = "待收货";
                }elseif($val['status']==5){
                    $val['status_name'] = "待评价";
                }elseif($val['status']==2){
                    $val['status_name'] = "待付款";
                }elseif($val['status']==10){
                    $val['status_name'] = "已取消";
                }
                $val['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
                $returninfo[$key] = $val;
            }
            $this->assign('_list', $returninfo);
        }
        $this->display();
    }
    //根据供应商获取商品列表
    public function goodlist(){

        $supplier_id = $_POST['supplier_id'];
        if($supplier_id==""){
            echo "";
            die();
        }
        $goodlist = M("Good")->where(array("supplier"=>$supplier_id))->field(array("id","title"))->select();
        if(count($goodlist) == 0){
            echo "";
            die();
        }
        echo $goodlist?json_encode($goodlist):"";
        die();
    }
    //导出数据导excel
    public function downloadexcel(){
        $selectsupplier = $_GET['supplier_id'];
        $selectstatus = $_GET['status'];
        $start_time = $_GET['start_time'];
        $end_time = $_GET['end_time'];
        $selectgood_id = $_GET['good_id'];
        $search = $_GET['search'];
        if($search== 1 && $selectsupplier == ""){
            $this->error("搜索的时候请选择供应商");
        }
        $memberinfo = M("Member")
            ->join("left join mt_auth_group_access on mt_member.uid=mt_auth_group_access.uid")
            ->where(array("mt_auth_group_access.group_id"=>"6"))->select();
        $memberdata = array();
        foreach($memberinfo as $key=>$val){
            $memberdata[$val['uid']] = $val['nickname'];
        }
        $goodlist = M("Good")->where(array("supplier"=>$selectsupplier))->select();
        $goodsql = "";
        foreach($goodlist as $key=>$val){
            $gooddata[$val['id']]['supplier'] = $val['supplier'];
            $gooddata[$val['id']]['good_name'] = $val['title'];
            //获取该供应商下面的商品
            $goodsql .= 'good_info like \'%"good_id":"'.$val['id'].'"%\' or good_info like \'%"good_id\":\"'.$val['id'].'\"%\' or';
        }
        $goodsql = substr($goodsql,0,-2);
        //
        if($selectstatus){
            $where["status"] = $selectstatus;
        }
        $where["_string"] = $goodsql;
        if($start_time){
            $where['create_time'] = array("between",array(strtotime($start_time),strtotime($end_time)));
        }

        if($selectsupplier) {
            $list = M("Invest")->where($where)->order("create_time desc")->select();
            $returninfo = array();
            foreach ($list as $key => $val) {
                $good_info = json_decode($val['good_info'], true);
                if (!is_array($good_info)) {
                    $good_info = json_decode($good_info, true);
                }
                $goodid = $good_info[0]['good_id'];
                $val['supplier_name'] = $memberdata[$gooddata[$goodid]['supplier']];
                $val['good_name'] = $gooddata[$goodid]['good_name'];
                $val['count'] = $good_info[0]['count'];
                if ($val['status'] == 1) {
                    $val['status_name'] = "完成";
                } elseif ($val['status'] == 3) {
                    $val['status_name'] = "待发货";
                } elseif ($val['status'] == 4) {
                    $val['status_name'] = "待收货";
                } elseif ($val['status'] == 5) {
                    $val['status_name'] = "待评价";
                } elseif ($val['status'] == 2) {
                    $val['status_name'] = "待付款";
                } elseif ($val['status'] == 10) {
                    $val['status_name'] = "已取消";
                }
                $val['create_time'] = date("Y-m-d H:i:s", $val['create_time']);
                $returninfo[$key] = $val;
            }
        }
        $date = date("Y-m-d");
        header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Transfer-Encoding: binary ");
        header("Content-Disposition:attachment;filename=supplier_$date.xls" );
        echo iconv("utf-8", "gb2312", "供应商名称")."\t";
        echo iconv("utf-8", "gb2312", "商品名称")."\t";
        echo iconv("utf-8", "gb2312", "件数")."\t";
        echo iconv("utf-8", "gb2312", "金额")."\t";
        echo iconv("utf-8", "gb2312", "操作时间")."\t";
        echo iconv("utf-8", "gb2312", "订单状态")."\t";
        echo "\n";
        foreach($returninfo as $key=>$val){
            echo iconv("utf-8", "gb2312",$val['supplier_name'])."\t";
            echo iconv("utf-8", "gb2312",$val['good_name'])."\t";
            echo iconv("utf-8", "gb2312",$val['count'])."\t";
            echo iconv("utf-8", "gb2312",$val['amount'])."\t";
            echo iconv("utf-8", "gb2312",$val['create_time'])."\t";
            echo iconv("utf-8", "gb2312",$val['status_name'])."\t";
            echo "\n";

         }
    }
}